@extends('admin::public.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <header class="title">编辑权限</header>
        </div>
        <div class="layui-card-body">
            <div class="layui-form">
                <input type="hidden" name="id" value="{{ $permission->id }}">
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">上级模块</label>
                    <div class="layui-input-block">
                        <select name="parent_id" lay-filter="parent">
                            <option value="0">请选择</option>
                            @forelse($permissions as $perm)
                                <option title="{{$perm['display_name']}}" value="{{$perm['id']}}"
                                        {{ isset($permission->id) && $perm['id'] == $permission->parent_id ? 'selected' : '' }}
                                        {{ isset($permissionId) && $perm['id'] == $permissionId ? 'selected' : ''}}
                                >{{$perm['display_name']}}</option>
                                @if(isset($perm['_child']))
                                    @foreach($perm['_child'] as $childs)
                                        <option title="{{$childs['display_name']}}" value="{{$childs['id']}}" {{ isset($permission->id) &&
                                        $childs['id'] == $permission->parent_id ? 'selected' : '' }}
                                                {{ isset($permissionId) && $childs['id'] == $permissionId ? 'selected' : ''}}
                                        >&nbsp;&nbsp;&nbsp;&nbsp;{{$childs['display_name']}}</option>
                                    @endforeach
                                @endif
                            @empty
                            @endforelse
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="" class="layui-form-label layui-required">模块配置</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" autocomplete="off" value="{{$permission->name??old('name')}}" class="layui-input" placeholder="如：system.manager">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="" class="layui-form-label layui-required">模块名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="display_name" id="display_name" autocomplete="off" value="{{$permission->display_name??old('display_name')}}" class="layui-input" placeholder="如：系统管理">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">模块路由</label>
                    <div class="layui-input-block">
                        <input class="layui-input" type="text" autocomplete="off" name="route" value="{{$permission->route??old('route')}}" placeholder="如：system.admin.list" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">模块排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" autocomplete="off" value="{{ $permission->sort ?? old('sort') }}" placeholder="数字越大排序越靠前" class="layui-input" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">模块图标</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" type="text" autocomplete="off" placeholder="请输入图标" name="icon" value="{{$permission->icon??old('icon')}}">
                    </div>
                    <div class="layui-input-inline">
                        <button type="button" class="layui-btn layui-btn-big" onclick="selectIcon()">选 择</button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn" lay-submit=""  lay-filter="submit">确 认</button>
                        {{--统一编辑时的返回操作--}}
                        @php( isset($permission) ? $permissionId = $permission->parent_id : null)
                        <a href="{{route('admin.permission')}}?permissionId={{isset($permissionId) ? $permissionId : 0}}" class="layui-btn layui-btn-primary"  id="returnPrevious" >返回</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
    <script>
        layui.use(['layer','form'],function () {
            var form = layui.form;
            var layer = layui.layer;
            form.on('select(parent)', function(data){
                var title = $(data.elem).find("option:selected").attr("title");
                $(data.elem).find("option:selected").text(title);
                form.render();
            });
            //选择图标
            window.selectIcon = function () {
                layer.open({
                    type: 2,
                    content: "{{route('admin.icons')}}",
                    title: '选择图标',
                    skin:'layui-skin',
                    area: ['700px', '530px'],
                    shade: 0.8,
                });
            }
            //选中回调
            window.callback = function (icon) {
                $('input[name="icon"]').val(icon);
            }
            //表单提交
            form.on('submit(submit)', function (data) {
                //验证表单
                if (!data.field.name) {
                    layer.tips('请输入模块配置', '#name', { tips: 3 });
                    return false;
                }
                if (!data.field.display_name) {
                    layer.tips('请输入模块名称', '#display_name', { tips: 3 });
                    return false;
                }
                //ajax提交
                $.ajax({
                    type: "PUT",
                    dataType: "json",
                    url: "{{route('admin.permission.update',['permission'=>$permission])}}",
                    data: data.field,
                    success: function (data) {
                        if (data.code == 1) {
                            layer.msg(data.msg, {
                                time: 2000
                            }, function () {
                                location.href = "{{route('admin.permission')}}";
                            })
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器开小差啦");
                    }
                });
            })
        });
    </script>
@endsection
